﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Models;
using System.Data;
using System.Collections.Generic;

public partial class Admin_store_StoreList : AdminBase
{

    private DataTable dc = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        grdbind();
    }

    public void grdbind()
    {
        using (Entities bll = new Entities())
        {
            var Bpl = from p in bll.S_Store.OrderByDescending(p => p.SID) select p;

            AspNetPager1.RecordCount = Bpl.Count();
            this.rptList.DataSource = Bpl.Skip((AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize).Take(AspNetPager1.PageSize).ToList();  //这里好像一定要Tolist();不然会有点错误；;
            this.rptList.DataBind();

        }
    }

    protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {   //当页面页数改变时，执行这个方法；
        AspNetPager1.CurrentPageIndex = e.NewPageIndex;
        grdbind();
    }

    //public string getHxNumber(string _SID)
    //{
    //    using (Entities bll = new Entities())
    //    {
    //        var StoreUser = bll.User.Where(p => p.UPower == _SID);
    //        List<string> _StoreUser = new List<string>();
    //        foreach (var SU in StoreUser)
    //        {
    //            _StoreUser.Add(SU.UOpenID);
    //        }

    //        var Bpl = from p in bll.S_Ordered
    //                  where _StoreUser.Contains(p.OHxOpenId)
    //                  select new { p.OID};
    //        return Bpl.Count().ToString();
    //    }
    //}


    protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string _order_id = ((Label)e.Item.FindControl("od_id")).Text;
            int order_id = Convert.ToInt32(_order_id);
            Repeater dl = (Repeater)e.Item.FindControl("rptList22"); //找到DataList1控件
            if (order_id != 0)
            {
                Entities MyDB = new Entities();
                var Bpl = from p in MyDB.S_Store_Item.Where(x => x.SID == order_id).OrderByDescending(p => p.SItemID) select p;
                dl.DataSource = Bpl.ToList();
                dl.DataBind();
            }
        }
    }

    protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Convert.ToInt32(((Label)e.Item.FindControl("lb_id")).Text);
        Entities bll = new Entities();
        S_Store model = bll.S_Store.First(p => p.SID == id);
        switch (e.CommandName.ToLower())
        {
            case "ibtnlock":
                if (model.IsLock == true)
                    model.IsLock = false;
                else
                    model.IsLock = true;
                break;
            case "del":
                bll.S_Store.Remove(model);
                break;
        }
        bll.SaveChanges();
        Response.Redirect("StoreList.aspx");

    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        grdbind();
    }
}

